package rabbitmq;

import com.rabbitmq.client.*;

import java.io.IOException;
import java.util.concurrent.TimeoutException;

/**
 * Created by User on 2017/12/23.
 */
public class Farmer {

    public static void main(String[] args) {
        new Farmer().transportGrain();

    }

    void transportGrain() {

        ConnectionFactory connectionFactory = new ConnectionFactory();
        connectionFactory.setHost("localhost");
        connectionFactory.setPort(5672);
        connectionFactory.setUsername("zql");
        connectionFactory.setPassword("123123");

        try (Connection connection = connectionFactory.newConnection(); Channel channel = connection.createChannel()) {
            // 表示声明exchange和queue   好像就是没有就创建
            channel.exchangeDeclare("grainExchange", BuiltinExchangeType.DIRECT, true, true, false, null);
            channel.queueDeclare("grainProcessor", true, false, false, null);

            // 将quue(grainProcessor)以代号routingKey(grainProcessorNameInGrainExchange)绑定到exchange(grainExchange)
            // queue和exchange之间的关系是多对多  一个queue可以绑定到多个exchange里   一个exchange里可以绑定多个queue
            // routingKey其实就是一个queue在一个exchange里叫啥名字
            channel.queueBind("grainProcessor", "grainExchange", "grainProcessorNameInGrainExchange");

            String grain = ",,,,,,,,,,,,,,,,,,,,,,,,";
            channel.basicPublish("grainExchange", "grainProcessorNameInGrainExchange", null, grain.getBytes());


        } catch (IOException | TimeoutException e) {
            e.printStackTrace();
        }
    }


}
